const app = getApp();
Page({
    data: {
        movies: [{
                url: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg'
            },
            {
                url: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg'
            },
            {
                url: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
            },
            {
                url: 'http://img02.tooopen.com/images/20141231/sy_78327074576.jpg'
            }
        ],
        showlogin: false,
        phone: '',
        password: '',
        content: '',
        iconfont: '',
        isHide: false,
        epanput: '',
        phInput: '',
        panput: '',
        searchmask: false,
        enterprisenid: '',
        enterprisename: '',
        enterprisenkye: '',
        items: [],
        SearchData: '',
        SearchType: false,
        scrollHeight: ''
    },
    searchCompany: function() {
        this.setData({
            searchmask: true
        })
    },
    cancel: function() {
        this.setData({
            searchmask: false
        })
    },
    wxSearchInput: function(e) {
        var _this = this;
        if (e.detail.value.length > 0) {
            _this.setData({
                SearchData: e.detail.value,
                SearchType: true,
            });
        } else {
            _this.setData({
                SearchData: '',
                SearchType: false,
            });
        }
        wx.request({
            url: app.data.api + 'user/getBusinessByLike',
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
                keyword: _this.data.SearchData
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                var data = res.data.data;
                var arr = []
                if (res.data.status == true) {
                    for (var i = 0; i < data.length; i++) {
                        arr.push(data[i])
                    }
                }
                _this.setData({
                    items: arr
                })
            }
        })
    },
    empty: function() {
        this.setData({
            SearchData: ''
        });
    },
    selectcontainer: function(e) {
        var data = e.currentTarget.dataset;
        this.setData({
            enterprisenid: data.id,
            enterprisename: data.name,
            enterprisenkye: data.kye,
            epanput: 'coror'
        });
        this.cancel();
    },
    phoneInput: function(e) {
        this.setData({
            phone: e.detail.value
        })
        if (e.detail.value.length > 0) {
            this.setData({
                phInput: 'coror'
            })
        } else {
            this.setData({
                phInput: ''
            })
        }
    },
    passwordInput: function(e) {
        let $this = this
        $this.setData({
            password: e.detail.value
        })
        if (e.detail.value.length > 0) {
            $this.setData({
                panput: 'coror'
            })
        } else {
            $this.setData({
                panput: ''
            })
        }
    },
    // 登录 
    login: function() {
        let $this = this;
        if ($this.data.enterprisename.length == 0) {
            $this.layerMsg(true, '企业名称不能为空', 'icon-weiwancheng');
            return false
        }
        if ($this.data.phone.length == 0) {
            $this.layerMsg(true, '用户名不能为空', 'icon-weiwancheng');
            return false
        }
        if ($this.data.password.length == 0) {
            $this.layerMsg(true, '密码不能为空', 'icon-weiwancheng');
            return false
        }
        wx.login({
            success: function(res) {
                wx.request({
                    url: app.data.api + '/user/userLogin',
                    data: {
                        bname: $this.data.enterprisenkye,
                        userName: $this.data.phone,
                        password: $this.data.password,
                        code: res.code
                    },
                    method: 'POST',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    success: function(res) {
                        if (res.data.status == true) {
                            app.data.register = res.data.status;
                            wx.showToast({
                                title: '登录成功',
                                icon: 'success',
                                duration: 2000
                            })
                            app.data.tokenID = res.data.data.token;
                            wx.setStorageSync('token', res.data.data.token)
                            wx.setStorageSync('openid', res.data.data.openid)
                            wx.setStorageSync('sessionKey', res.data.data.sessionKey)
                            wx.switchTab({
                                url: '../index/index'
                            })
                        } else {
                            $this.layerMsg(true, res.data.msg, 'icon-weiwancheng');
                        }
                    }
                })
            }
        })
    },
    layerMsg: function(msgtype, msg, iconfont) {
        let $this = this;
        if (msgtype == true) {
            $this.setData({
                isHide: msgtype,
                content: msg,
                iconfont: iconfont
            })
            setTimeout(function() {
                $this.setData({
                    isHide: false
                })
            }, 2000)
        } else {}
    },
    onLoad: function() {
        var Height = wx.getSystemInfoSync().windowHeight - 50;
        this.setData({
            scrollHeight: Height,
        })
        var token = wx.getStorageSync('token')
        if (token == '') {
            wx.switchTab({
                url: '../login/login'
            })
            this.setData({
                showlogin: true
            })
        } else {
            wx.switchTab({
                url: '../index/index'
            })
        };
    }
})